package br.com.light.adapter.envio.copia;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.DOMOutputter;
import org.jdom.output.XMLOutputter;

import com.sap.aii.mapping.api.StreamTransformation;
import com.sap.aii.mapping.api.StreamTransformationException;

public class MappingEnvioCopiaRPS implements StreamTransformation {

	/**
	 * @param args
	 * @throws FileNotFoundException 
	 * @throws StreamTransformationException 
	 */
	public static void main(String[] args) throws FileNotFoundException, StreamTransformationException {
		File file = new File("D:/documentos/Complex/Light/PedidoEnvioCopiaRPS.xml");
		InputStream in = new FileInputStream(file);
			
		File fileOut = new File("D:/documentos/Complex/Light/PedidoEnvioCopiaRPSRFC.xml");
		OutputStream out = new FileOutputStream(fileOut);
		
		new MappingEnvioCopiaRPS().execute(in, out);

	}

	public void execute(InputStream in, OutputStream out)
			throws StreamTransformationException {
		
		try {
			Element header = new Element("ZNFSEF_SHIPMENT_CANCEL_IN_SP", "ns1", "urn:sap-com:document:sap:rfc:functions");
			
			Element it_alerta = new Element("IT_ALERTA");
			header.addContent(it_alerta);
			Element item_alerta = new Element("item");
			it_alerta.addContent(item_alerta);
			Element error_code_alerta = new Element("ERROR_CODE");
			item_alerta.addContent(error_code_alerta);
			Element message_alerta = new Element("MESSAGE");
			item_alerta.addContent(message_alerta);
			Element correction_alerta = new Element("CORRECTION");
			item_alerta.addContent(correction_alerta);		
			
			Element it_error = new Element("IT_ERROR");
			header.addContent(it_error);
			Element item_error = new Element("item");
			it_error.addContent(item_error);
			Element error_code = new Element("ERROR_CODE");
			item_error.addContent(error_code);
			Element message_error = new Element("MESSAGE");
			item_error.addContent(message_error);
			Element correction_error = new Element("CORRECTION");
			item_error.addContent(correction_error);
			
			Element it_error_lote = new Element("IT_ERROR_LOTE");
			header.addContent(it_error_lote);
			Element item_error_lote = new Element("item");
			it_error_lote.addContent(item_error_lote);
			Element error_code_lote = new Element("ERROR_CODE");
			item_error_lote.addContent(error_code_lote);
			Element message_error_lote = new Element("MESSAGE");
			item_error_lote.addContent(message_error_lote);
			Element correction_error_lote = new Element("CORRECTION");
			item_error_lote.addContent(correction_error_lote);
			
			
			Element it_nfse = new Element("IT_NFSE");
			header.addContent(it_nfse);
			Element item_nfse = new Element("item");
			it_nfse.addContent(item_nfse);
			Element mandt = new Element("MANDT");
			item_nfse.addContent(mandt);
			Element docnum = new Element("DOCNUM");
			item_nfse.addContent(docnum);
			Element nfsenum = new Element("NFSENUM");
			item_nfse.addContent(nfsenum);
			Element vercode = new Element("VERCODE");
			item_nfse.addContent(vercode);
			Element issdate = new Element("ISSDATE");
			item_nfse.addContent(issdate);
			Element isstime = new Element("ISSTIME");
			item_nfse.addContent(isstime);
			Element cancode = new Element("CANCODE");
			item_nfse.addContent(cancode);
			Element candate = new Element("CANDATE");
			item_nfse.addContent(candate);
			Element cantime = new Element("CANTIME");
			item_nfse.addContent(cantime);
			Element stat_out = new Element("STAT_OUT");
			item_nfse.addContent(stat_out);
			Element stat_in = new Element("STAT_IN");
			item_nfse.addContent(stat_in);
			Element shipnum = new Element("SHIPNUM");
			item_nfse.addContent(shipnum);
			Element rpsnum = new Element("RPSNUM");
			item_nfse.addContent(rpsnum);
			Element ship_prot = new Element("SHIP_PROT");
			item_nfse.addContent(ship_prot);
			Element docsub = new Element("DOCSUB");
			item_nfse.addContent(docsub);	
			Element docori = new Element("DOCORI");
			item_nfse.addContent(docori);
			Element cod_erro = new Element("COD_ERRO");
			item_nfse.addContent(cod_erro);	
			Element signature = new Element("SIGNATURE");
			item_nfse.addContent(signature);
			Element opsimples = new Element("OPSIMPLES");
			item_nfse.addContent(opsimples);
			Element numguia = new Element("NUMGUIA");
			item_nfse.addContent(numguia);
			Element dtquitguia = new Element("DTQUITGUIA");
			item_nfse.addContent(dtquitguia);
			Element amnt_credito = new Element("AMNT_CREDITO");
			item_nfse.addContent(amnt_credito);
	
			Element i_id_sist = new Element("I_ID_SIST");
			header.addContent(i_id_sist);
			
			Element i_shipnum = new Element("I_SHIPNUM");
			header.addContent(i_shipnum);
			
			Element i_ship_inf = new Element("I_SHIP_INF");
			header.addContent(i_ship_inf);
			Element ship_im = new Element("IM");
			i_ship_inf.addContent(ship_im);
			
			Element i_ship_prot = new Element("I_SHIP_PROT");
			header.addContent(i_ship_prot);
			
			Element i_xml_string = new Element("I_XML_STRING");
			header.addContent(i_xml_string);
			
			XMLOutputter output = new XMLOutputter();
			StringBuffer buffer = new StringBuffer();
			SAXBuilder builder = new SAXBuilder();
			Document doc = builder.build(in);
			Element root = doc.getRootElement();
			List elements = root.getChildren();
			Iterator iter = elements.iterator();
			
			while (iter.hasNext()) {
				Element element = (Element) iter.next();
				System.out.println(element.getName());
				if("Message1".equals(element.getName())){
					List message1Childs = element.getChildren();
					Iterator messageChildIter = message1Childs.iterator();
					
					while (messageChildIter.hasNext()) {
						Element messageChildElement = (Element) messageChildIter.next();
						
						List znfseList = messageChildElement.getChildren();
						Iterator znfseIter = znfseList.iterator();
						
						while(znfseIter.hasNext()){
							Element znfseChild = (Element)znfseIter.next();
							System.out.println(znfseChild.getName());
							if("IT_ALERTA".equals(znfseChild.getName())){	
								
								List itemList = znfseChild.getChildren();
								Iterator itemIter = itemList.iterator();
								
								while(itemIter.hasNext()){
									Element itemChild = (Element)itemIter.next();
									String error_code_alerta_in = itemChild.getChildText("ERROR_CODE");
									error_code_alerta.setText(error_code_alerta_in);
									
									String message_alerta_in = itemChild.getChildText("MESSAGE");
									message_alerta.setText(message_alerta_in);
									
									String correction_alerta_in = itemChild.getChildText("CORRECTION");
									correction_alerta.setText(correction_alerta_in);
								}
							}
							if("IT_ERROR".equals(znfseChild.getName())){	
								
								List itemList = znfseChild.getChildren();
								Iterator itemIter = itemList.iterator();
								
								while(itemIter.hasNext()){
									Element itemChild = (Element)itemIter.next();
									String error_code_in = itemChild.getChildText("ERROR_CODE");
									error_code.setText(error_code_in);
									
									String message_error_in = itemChild.getChildText("MESSAGE");
									message_error.setText(message_error_in);
									
									String correction_error_in = itemChild.getChildText("CORRECTION");
									correction_error.setText(correction_error_in);
								}
							}
							if("IT_ERROR_LOTE".equals(znfseChild.getName())){	
								
								List itemList = znfseChild.getChildren();
								Iterator itemIter = itemList.iterator();
								
								while(itemIter.hasNext()){
									Element itemChild = (Element)itemIter.next();
									String error_code_lote_in = itemChild.getChildText("ERROR_CODE");
									error_code_lote.setText(error_code_lote_in);
									
									String message_error_lote_in = itemChild.getChildText("MESSAGE");
									message_error_lote.setText(message_error_lote_in);
									
									String correction_error_lote_in = itemChild.getChildText("CORRECTION");
									correction_error_lote.setText(correction_error_lote_in);
								}
							}
							if("IT_NFSE".equals(znfseChild.getName())){	
								
								List itemList = znfseChild.getChildren();
								Iterator itemIter = itemList.iterator();
								
								while(itemIter.hasNext()){
									Element itemChild = (Element)itemIter.next();
									String mandt_in = itemChild.getChildText("MANDT");
									mandt.setText(mandt_in);
									String docnum_in = itemChild.getChildText("DOCNUM");
									docnum.setText(docnum_in);
									String nfsenum_in = itemChild.getChildText("NFSENUM");
									nfsenum.setText(nfsenum_in);
									String vercode_in = itemChild.getChildText("VERCODE");
									vercode.setText(vercode_in);
									String issdate_in = itemChild.getChildText("ISSDATE");
									issdate.setText(issdate_in);
									String isstime_in = itemChild.getChildText("ISSTIME");
									isstime.setText(isstime_in);
									String cancode_in = itemChild.getChildText("CANCODE");
									cancode.setText(cancode_in);
									String candate_in = itemChild.getChildText("CANDATE");
									candate.setText(candate_in);
									String cantime_in = itemChild.getChildText("CANTIME");
									cantime.setText(cantime_in);
									String stat_out_in = itemChild.getChildText("STAT_OUT");
									stat_out.setText(stat_out_in);
									String stat_in_in = itemChild.getChildText("STAT_IN");
									stat_in.setText(stat_in_in);
									String shipnum_in = itemChild.getChildText("SHIPNUM");
									shipnum.setText(shipnum_in);
									String rpsnum_in = itemChild.getChildText("RPSNUM");
									rpsnum.setText(rpsnum_in);
									String ship_prot_in = itemChild.getChildText("SHIP_PROT");
									ship_prot.setText(ship_prot_in);
									String docsub_in = itemChild.getChildText("DOCSUB");
									docsub.setText(docsub_in);
									String docori_in = itemChild.getChildText("DOCORI");
									docori.setText(docori_in);
									String cod_erro_in = itemChild.getChildText("COD_ERRO");
									cod_erro.setText(cod_erro_in);
									String signature_in = itemChild.getChildText("SIGNATURE");
									signature.setText(signature_in);
									String opsimples_in = itemChild.getChildText("OPSIMPLES");
									opsimples.setText(opsimples_in);
									String numguia_in = itemChild.getChildText("NUMGUIA");
									numguia.setText(numguia_in);
									String dtquitguia_in = itemChild.getChildText("DTQUITGUIA");
									dtquitguia.setText(dtquitguia_in);
									String amnt_credito_in = itemChild.getChildText("AMNT_CREDITO");
									amnt_credito.setText(amnt_credito_in);
								
								}
							}
							
							String i_id_sist_in = messageChildElement.getChildText("I_ID_SIST");
							i_id_sist.setText(i_id_sist_in);
							
							String i_shipnum_in = messageChildElement.getChildText("I_SHIPNUM");
							i_shipnum.setText(i_shipnum_in);
							
							
							if("I_SHIP_INF".equals(znfseChild.getName())){
								String ship_im_in = znfseChild.getChildText("IM");
								ship_im.setText(ship_im_in);
							}
							
							String i_ship_prot_in = messageChildElement.getChildText("I_SHIP_PROT");
							i_ship_prot.setText(i_ship_prot_in);
						}
								
						
					}
					
					
				}
				
				if("Message2".equals(element.getName())){
					List message1Childs = element.getChildren();
					Iterator messageChildIter = message1Childs.iterator();
					while (messageChildIter.hasNext()) {
						Element elemMsg2 = (Element)messageChildIter.next();
						String tags = output.outputString(elemMsg2);
						buffer.append(tags);
					}
					i_xml_string.setText(buffer.toString());
					
					
				}
				
				
				
				
			}
			
			Document jdoc = new Document(header);
			
			DOMOutputter outputter = new DOMOutputter();
			org.w3c.dom.Document docNative = outputter.output(jdoc);
			
			TransformerFactory tf=TransformerFactory.newInstance();
			Transformer transform=tf.newTransformer();
		
		transform.transform(new DOMSource(docNative), new StreamResult(out));
		
	}catch (JDOMException e){
		e.printStackTrace();
	} catch (IOException e) {// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (TransformerConfigurationException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (TransformerException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
		
		
	}

	public void setParameter(Map map) {
		// TODO Auto-generated method stub
		
	}

}
